package cn.shiwei.hr.web.controller;

import cn.shiwei.hr.domain.Course;
import cn.shiwei.hr.dto.CourseKillDto;
import cn.shiwei.hr.service.IKillCourseService;
import cn.shiwei.hr.domain.KillCourse;
import cn.shiwei.hr.query.KillCourseQuery;
import cn.shiwei.hr.repository.AjaxResult;
import cn.shiwei.hr.repository.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/killCourse")
public class KillCourseController {
    @Autowired
    public IKillCourseService killCourseService;

    /**
    * 保存和修改
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody KillCourse killCourse){
        if(killCourse.getId()!=null){
            killCourseService.updateById(killCourse);
        }else{
            killCourseService.insert(killCourse);
        }
        return AjaxResult.me();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        killCourseService.deleteById(id);
        return AjaxResult.me();
    }

   /**
   * 根据ID获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("id")Long id){
        return AjaxResult.me().setResultObj(killCourseService.selectById(id));
    }


    /**
    * 获取所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public AjaxResult list(){
        return AjaxResult.me().setResultObj(killCourseService.selectList(null));
    }

    /**
    * 分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public AjaxResult json(@RequestBody KillCourseQuery query){
        Page<KillCourse> page = new Page<KillCourse>(query.getPage(),query.getRows());
        page = killCourseService.selectPage(page);
        PageList pageList = new PageList<KillCourse>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }

    @PostMapping(value = "/online")
    public AjaxResult json(){
        killCourseService.online(new KillCourse());
        return AjaxResult.me();
    }

    @GetMapping(value = "/killList")
    public AjaxResult getList() {
        return AjaxResult.me().setResultObj(killCourseService.list());
    }

    /**
     * @param id 课程id
     * @return
     */
    @GetMapping(value = "/one/{id}")
    public AjaxResult getOne(@PathVariable Long id){
        return AjaxResult.me().setResultObj(killCourseService.getOne(id));
    }

    @PostMapping(value = "/kill")
    public AjaxResult startSpike(@RequestBody CourseKillDto courseKillDto){
        return AjaxResult.me().setResultObj(killCourseService.startSpike(courseKillDto));
    }




}
